package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mapper.LoginMapper;
import com.itheima.pojo.BsUser;
import com.itheima.pojo.PePermission;
import com.itheima.pojo.PePermissionGroup;
import com.itheima.service.LoginService;
import com.itheima.util.LoginResult;
import com.itheima.util.LoginResultDto;
import com.itheima.util.PageResult;
import com.itheima.util.SimpleResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private LoginMapper loginMapper;
    @Override
    public BsUser login(String username, String newpassword) {
        LambdaQueryWrapper<BsUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BsUser::getPassword,newpassword);
        wrapper.eq(BsUser::getEmail,username);
        BsUser bsUser = loginMapper.selectOne(wrapper);
        return bsUser;
    }

    @Override
    public void savetoken(BsUser bsUser,String token) {
        Integer id = bsUser.getId();
        loginMapper.savetoken(id,token);
    }

    //修改用户的信息
    @Override
    public void UpdateUser(BsUser bsuser) {
        loginMapper.updateById(bsuser);
    }
    //修改页面用户信息回显
    @Override
    public BsUser UpdateShow(Integer id) {
        BsUser bsUser = loginMapper.selectById(id);
        return bsUser;
    }
    //分页条件查询
    @Override
    public PageResult findPage(Integer page, Integer pagesize, String username) {
        Page<BsUser> p = new Page<>(page, pagesize);
        LambdaQueryWrapper<BsUser> wrapper = new LambdaQueryWrapper<>();
        if(username!=null){
            wrapper.like(BsUser::getUsername,username);
        }
        Page<BsUser> selectPage = loginMapper.selectPage(p, wrapper);
        List<BsUser> records = selectPage.getRecords();
        for (BsUser bsUser : records) {
            String name = loginMapper.selectpgi(bsUser.getPermission_group_id());
            bsUser.setPermission_group_title(name);
        }
        selectPage.setRecords(records);
        Integer count = loginMapper.selectCount(wrapper);

        PageResult pageResult = new PageResult();
        pageResult.setList(selectPage.getRecords());
        pageResult.setCounts(count);
        pageResult.setPage(page+"");
        pageResult.setPagesize(pagesize+"");
        pageResult.setPages(Integer.parseInt(selectPage.getPages()+""));
        return pageResult;
    }
    //根据id删除
    @Override
    public void deleteById(Integer id) {
        loginMapper.deleteById(id);
    }
    //用户启用和禁用
    @Override
    public void updateAbled(Integer id, boolean disabled) {
        BsUser bsUser = loginMapper.selectById(id);
        if(disabled){
            bsUser.setIs_deleted(1);
        }else if(disabled){
            bsUser.setIs_deleted(0);
        }
        loginMapper.updateById(bsUser);
    }
    //新增用户
    @Override
    public void add(BsUser bsUser) {
        loginMapper.insert(bsUser);
    }
    //用户简单列表
//    @Override
//    public List<SimpleResult> simple(String keyword, boolean disabled) {
//        LambdaQueryWrapper<BsUser> wrapper = new LambdaQueryWrapper<>();
//        List<PePermissionGroup> list = loginMapper.selectpe_permission_group();
//        List<SimpleResult> list2 = new ArrayList<>();
//        for (PePermissionGroup group : list) {
//            SimpleResult result = new SimpleResult();
//            result.setId(group.getId());
//            result.setTitle(group.getName());
//            list2.add(result);
//        }
//        if(disabled){
//            wrapper.like(BsUser::getIntroduction,keyword).or().like(BsUser::getUsername,keyword);
//            wrapper.eq(BsUser::getIs_deleted,1);
//            List<BsUser> bsUsers = loginMapper.selectList(wrapper);
//            for (BsUser bu:bsUsers) {
//                SimpleResult simpleResult = new SimpleResult();
//                simpleResult.setId(bu.getId());
//                simpleResult.setTitle(bu.getUsername());
//                list.add(simpleResult);
//            }
//        }else{
//            wrapper.like(BsUser::getIntroduction,keyword).or().like(BsUser::getUsername,keyword);
//            wrapper.eq(BsUser::getIs_deleted,0);
//            List<BsUser> bsUsers = loginMapper.selectList(wrapper);
//            for (BsUser bu:bsUsers) {
//                SimpleResult simpleResult = new SimpleResult();
//                simpleResult.setId(bu.getId());
//                simpleResult.setTitle(bu.getUsername());
//                list.add(simpleResult);
//            }
//        }
//        return list2;
//    }

    @Override
    public void updatepassword(Integer id, String password) {
        BsUser bsUser = loginMapper.selectById(id);
        bsUser.setPassword(password);
        loginMapper.updateById(bsUser);
    }

    @Override
    public LoginResult profile(String admintoken) {
        LoginResult loginResult = new LoginResult();

        Integer bsUserId = loginMapper.selectprofig(admintoken);
        BsUser bsUser = loginMapper.selectById(bsUserId);
        Long groupid = bsUser.getPermission_group_id();
        List<String> selectpermission = loginMapper.selectmenus(groupid);

        LoginResultDto loginResultDto = new LoginResultDto();
        List<String> menu = new ArrayList<>();
        List<String> point = new ArrayList<>();
        for (String permission:selectpermission) {
            PePermission pp = loginMapper.selectPermission(permission);
            if(pp.getType()==1){
                menu.add(pp.getPermissionMenuExtendId()+"");
            }else if(pp.getType()==2){
                point.add(pp.getPermissionPointExtendId()+"");
            }
        }
        loginResultDto.setMenus(menu);
        loginResultDto.setPoints(point);

        loginResult.setName(bsUser.getUsername());
        loginResult.setAvatar(bsUser.getAvatar());
        loginResult.setIntroduction(bsUser.getIntroduction());
        loginResult.setRole(bsUser.getRole());
        loginResult.setRoles(loginResultDto);

        return loginResult;
    }
    //登出
    @Override
    public void logout(String s) {
        loginMapper.deletetoken(s);
    }
}
